package com.yidu.t299.controller.ssk;
import com.yidu.t299.domain.ssk.School;
import com.yidu.t299.domain.ssk.StuReturnMoney;
import com.yidu.t299.service.ssk.StuReturnMoneyBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 粟术康
 * @version 1.0
 * @description
 * @date:2023/3/17 14:32
 */
@RestController
public class StuReturnMoneyController {
    @Autowired
    private StuReturnMoneyBiz stuReturnMoneyBiz;


    /**
     * 初始化操作，跳转并保存下拉框数据(实现动态下拉框功能)
     */
    @RequestMapping("stureturnmoney/init")
    public ModelAndView saveSelect(){
        List<School> schoolList = stuReturnMoneyBiz.selectAllGetSchool();//得到所有校区信息
        ModelAndView mv = new ModelAndView();
        mv.setViewName("StuReturnMoneyCRUD");//跳转的页面
        mv.addObject("schoolList",schoolList); //保存校区信息
        return mv;
    }

    /**
     * 分页查询显示数据在表格中
     *
     * @param limit 页码
     * @param page  每页的行数
     * @param stuReturnMoney   接收多条件搜索的表单数据对象
     * @return 映射集合对象
     */
    @GetMapping("stureturnmoney/restful_stureturnmoney")
    public Map<String, Object> findAll(int limit, int page, StuReturnMoney stuReturnMoney) {
        // 根据account对象得到条件字符串
        String condition = stuReturnMoneyBiz.getCondition(stuReturnMoney);
        // 按条件统计总记录行数
        int count = stuReturnMoneyBiz.selectCount(condition);
        // 按条件分页查询
        List<StuReturnMoney> stuReturnMoneyList = stuReturnMoneyBiz.selectByPage(limit, page-1, condition);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为退费对象集合的映射
        mapData.put("data", stuReturnMoneyList);
        //把映射集合响应到客户端
        return mapData;
    }

    /**
     * 添加一个新退费
     *
     * @param stuReturnMoney 退费对象：其前要跟注解 @RequestBody，因为前端页面提交过来的数据就是JSON格式的字符串
     * @return 提示信息字符串
     */
    @PostMapping("stureturnmoney/restful_stureturnmoney")
    public String addEmp(@RequestBody StuReturnMoney stuReturnMoney) {
        System.out.println(stuReturnMoney);
        if(stuReturnMoney!=null){
            if(stuReturnMoney.getStudentName()!=null  && stuReturnMoney.getAccountType()!=null && stuReturnMoney.getSchoolName()!=null && stuReturnMoney.getRecordName()!=null && stuReturnMoney.getClassName()!=null){
                stuReturnMoney.setStudentId(stuReturnMoneyBiz.selectByStudentName(stuReturnMoney.getStudentName()));
                stuReturnMoney.setAccountId(stuReturnMoneyBiz.selectByAccountType(stuReturnMoney.getAccountType()));
                stuReturnMoney.setSchoolId(stuReturnMoneyBiz.selectBySchoolName(stuReturnMoney.getSchoolName()));
                stuReturnMoney.setRecordId(stuReturnMoneyBiz.selectByRecordName(stuReturnMoney.getRecordName()));
                stuReturnMoney.setClassId(stuReturnMoneyBiz.selectByClassName(stuReturnMoney.getClassName()));
                //调用业务层实现新增退费，并返回正确与否字符串
                return stuReturnMoneyBiz.insert(stuReturnMoney) > 0 ?  "add-success" : "add-failure";
            }
        }
        //调用业务层实现新增退费，并返回正确与否字符串
        return "add-failure";
    }

    /**
     * 修改退费信息
     *
     * @param stuReturnMoney 退费对象
     * @return 提示信息字符串
     */
    @PutMapping("stureturnmoney/restful_stureturnmoney")
    public String updateEmp(@RequestBody StuReturnMoney stuReturnMoney) {
        System.out.println(stuReturnMoney);
        if(stuReturnMoney!=null){
            if(stuReturnMoney.getStudentName()!=null  && stuReturnMoney.getAccountType()!=null && stuReturnMoney.getSchoolName()!=null && stuReturnMoney.getRecordName()!=null && stuReturnMoney.getClassName()!=null){
                stuReturnMoney.setStudentId(stuReturnMoneyBiz.selectByStudentName(stuReturnMoney.getStudentName()));
                stuReturnMoney.setAccountId(stuReturnMoneyBiz.selectByAccountType(stuReturnMoney.getAccountType()));
                stuReturnMoney.setSchoolId(stuReturnMoneyBiz.selectBySchoolName(stuReturnMoney.getSchoolName()));
                stuReturnMoney.setRecordId(stuReturnMoneyBiz.selectByRecordName(stuReturnMoney.getRecordName()));
                stuReturnMoney.setClassId(stuReturnMoneyBiz.selectByClassName(stuReturnMoney.getClassName()));
                //调用业务层实现新增退费，并返回正确与否字符串
                return stuReturnMoneyBiz.updateByPrimaryKey(stuReturnMoney) > 0 ? "update-success" : "update-failure";
            }
        }
        //调用业务层实现修改退费，并返回正确与否字符串
        return "update-failure";
    }

    /**
     * 根据退费编号删除
     *
     * @param returnMoneyId 退费编号
     * @return 提示信息字符串
     */
    @DeleteMapping("stureturnmoney/restful_stureturnmoney/{returnMoneyId}")
    public String deleteEmpById(@PathVariable("returnMoneyId") int returnMoneyId) {
        //调用业务层实现删除校区，并返回正确与否字符串
        return stuReturnMoneyBiz.deleteByPrimaryKey(returnMoneyId) > 0 ? "delete-success" : "delete-failure";
    }

    /**
     * 根据多个账退费编号批量删除退费
     * 此方法使用put请求替代delete，put可以带请求体
     *
     * @param returnMoneyIdStr 多个退费编号字符串
     * @return 提示信息字符串
     */
    @PutMapping("stureturnmoney/batchDel")
    public String deleteEmpByManyId(@RequestParam String returnMoneyIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = returnMoneyIdStr.split(",");
        //循环按账户编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (stuReturnMoneyBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "delete-failure";
            }
        }
        return "delete-success";
    }
}
